//
//  StatusViewModel.swift
//  WeiBo
//
//  Created by Tony on 2018/1/22.
//  Copyright © 2018年 Tony. All rights reserved.
//

import UIKit

/* 视图模型封装 */
class StatusViewModel: NSObject {
    
    // 常规属性
    var status : Status?
    
    // 数据处理
    var sourceText: String?                 // 处理微博来源
    var created_atText: String?             // 处理微博创建时间
    var verified_image : UIImage?           // 处理用户认证图标
    var vip_image : UIImage?                // 处理用户会员等级
    var profileUrl : URL?                   // 处理用户头像URL
    
 
 
    init(status : Status) {
        self.status = status
        
        // 处理微博来源
        if let source = status.source, source != ""  {
            let startIndex = (source as NSString).range(of: ">").location + 1
            let length = (source as NSString).range(of: "</").location - startIndex
            
            sourceText = (source as NSString).substring(with: NSRange.init(location: startIndex, length: length))
        }
        
        // 处理微博创建时间
        if let created_at = status.created_at {
             created_atText = Date.createDateString(created_at)
        }
        
        // 处理用户认证图标
        let verified_type = status.user?.verified_type ?? -1
        switch verified_type {
            case 0:
            verified_image = UIImage.init(named: "avatar_vip")
            case 2,3,5:
            verified_image = UIImage.init(named: "avatar_grassroot")
            case 220:
            verified_image = UIImage.init(named: "avatar_enterprise_vip")
            default:
            verified_image = nil
        }

        // 处理用户会员等级
        let mbrank = status.user?.mbrank ?? 0
        if mbrank > 0 && mbrank <= 6 {
            vip_image = UIImage.init(named: "common_icon_membership_level\(mbrank)")
        }
        
        // 处理用户头像URL
        let profileUrlStr = status.user?.profile_image_url ?? ""
         profileUrl = URL.init(string: profileUrlStr)

    }
    
}
